<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="100px" size="mini">
      <el-row :gutter="40">
        <el-col :span="6">
          <el-form-item label="审批意见" prop="opinion">
            <el-radio-group v-model="formItem.opinion" @change="radioChange">
              <el-radio label="PASS">同意</el-radio>
              <el-radio v-if="canreject" label="REJECT">不同意</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述" prop="reason">
            <el-input v-model="formItem.reason" placeholder="请输入审批描述" :maxlength="1023" type="textarea" :rows="rows" :autosize="{ minRows: 3, maxRows: 5}" resize="none" />
          </el-form-item>
        </el-col>
        <el-col :offset="20" :span="4">
          <el-button size="mini" @click="cancel">取消</el-button>
          <el-button size="mini" type="warning" @click="ok">确认</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import {
  create, detail
} from '@neu/dcam-ui/api/processApprove'
const formItem = {
  reason: '通过',
  opinion: 'PASS'
}
export default {
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    },
    canreject: {
      required: false,
      type: Boolean,
      default: true
    }
  },

  data() {
    return {
      titles: ['待选意见', '已选择'],
      showCause: false,
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      rows: 2,
      // 表单验证规则
      rules: {
        reason: [{
          required: true,
          message: '描述不能为空',
          trigger: 'change'
        }],
        opinion: [{
          required: true,
          message: '审批意见不能为空',
          trigger: 'change'
        }]
      }
    }
  },
  created() {
    detail(this.workOrderNum, this.taskId).then(res => {})
  },
  methods: {
    radioChange(v) {
      if (v === 'PASS') {
        this.formItem.reason = '通过'
      } else {
        this.formItem.reason = ''
      }
    },
    /** 确认 */
    ok(value) {
      const saveData = {
        opinion: this.formItem.opinion,
        reason: this.formItem.reason,
        taskId: this.taskId,
        workOrderNum: this.workOrderNum
      }
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            this.formItem.workOrderNum = this.workOrderNum
            this.formItem.taskId = this.taskId
            create(saveData).then(res => {
              if (res.status === 'success') {
                this.cancel()
              }
            })
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    /** 取消 */
    cancel() {
      this.$emit('cancelNewOrderForm', 'refresh')
    }
  }
}
</script>
